<!DOCTYPE html>
<!--
# Copyright (c) 2014-2021 Contributors to the Eclipse Foundation
# 
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
# 
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0
# 
# SPDX-License-Identifier: EPL-2.0
#
-->
<html lang="en-US">
<head>
<title>IBM MessageSight REST Message client</title>
<link rel="stylesheet" href="css/messageSightSamples.css" />
<link rel="stylesheet" href="css/messagingREST.css" />

<script type="text/javascript" src="js/restmsg.js"></script>
<script type="text/javascript">

/** The currently selected tab. */
var currentTab;


/**
 * Create a client object based on the form data.
 */
function initialize() {
	// trace("enter: initialize");
    var topicForm = document.getElementById('topicForm');
    var host = topicForm.host.value;
    var port = parseInt(topicForm.port.value);
    var path = topicForm.path.value;
    var protocol = (topicForm.ssl.checked) ? "https" : "http";
	var clientid = topicForm.clientId.value;
	var username = topicForm.userName.value;
	var password = topicForm.password.value;
	var client = new MessagingREST.Client(host, port, path, protocol, clientid, username, password);
	client.setTrace(trace);
	return client;
}


/**
 * Invoke request to obtained retained message
 */
function retrieveRetainedMessage() {
	// trace("enter: retrieveRetainedMessage");
    // GET /restmsg/message/topicName
    var client = this.initialize();
    var topicForm = document.getElementById('topicForm');
    var topiocName = topicForm.retainedMsgTopic.value;
    client.getRetainedMsg(topiocName,  this.successGetAlert, this.failureAlert);
}

/**
 * Delete retained message
 */
function deleteRetainedMsg() {
	// trace("enter: deleteRetainedMsg");
    // PUT /restmsg/message/topicName
    var client = this.initialize();
    var topicForm = document.getElementById('topicForm');
    var topiocName = topicForm.retainedMsgTopic.value;
    client.deleteRetained(topiocName, this.successAlert, this.failureAlert);
}
 
/**
 * Send a message to a topic
 */
function publishMessageTopic() {
	// trace("Enter: publishMessageTopic");
    // POST /restmsg/message/topicname?persist=bool
    var client = this.initialize();		
    var topicForm = document.getElementById('topicForm');
    var topicName = topicForm.topicName.value;
    client.publishMessageTopic(topicName, topicForm.publishPersist, topicForm.publishRetain, this.successAlert, this.failureAlert);
}

/**
 * HTML Application logic.
 */
function onLoad() {
    var form = document.getElementById("topicForm");    
    form.clientId.value = "";
    form.host.value = "127.0.0.1";
    form.port.value =  16102;
    form.path.value = "/restmsg";
    traceClear();
}

/*
 * failure callback
 */
function failureAlert(status, message) {
	trace("Operation failed with code=" + status + "  message=" + message);
}

/*
 * success callback
 */
function successAlert(message) {
	trace("Operation success: " + message);
}

/*
 * success on get callback
 */
function successGetAlert(topic, message) {
    trace("Get success: topic="+topic + " message="+message);
}

/**
 * Display message received by subscription
 */
function displayMessage(topic, retained, message) {
    var form = document.getElementById("topicForm");
    form.receivedTopicName.value = topic;
    form.receivedRetained.value = retained;
    form.receivedMessage.value = message;
}

function traceClear() {
	document.getElementById('sessionTrace').value = '';
}

function trace(msg) {
	var traceArea = document.getElementById('sessionTrace');
	var val = traceArea.value;
	if (val.length > 0) {
		traceArea.value = val + "\n" + msg;
	} else {
		traceArea.value = msg;
	}
}


function onUnload() {
}


</script>
</head>
<body onload="onLoad()" onunload="onUnload()">
	<div class="demoHeader">
		<div class="demoHeaderPrimary">
			<div class="demoHeaderPrimaryInner">
				<span>
					<div class="demoHeaderPrimaryTitle">IBM MessageSight</div>
				</span>
			</div>
			<span>
				<div class="demoHeaderLogoBox">
					<div class="demoHeaderIBMLogo" alt="IBM�"></div>
				</div>
				<div class="demoHeaderHelpLogo" id="help" alt="Help" onclick="help()"></div>
			</span>
		</div>
		<div class="demoBlueLip"></div>
	</div>
	<div class="demoTitleContainer">
		<span class="demoTitle">IBM MessageSight REST Message client - v1.0</span>
	</div>
	<div class="demoGrayLip"></div>
	<div class="demoContentContainer">

	<form id="topicForm" class="tabContent">
		<fieldset>
			<legend>Connection</legend>
				
			<label for="topicServer">Server</label>
			<input id="topicServer" type="text" name="host" size="30" />
			 
			<label for="topicPort">Port</label>
			<input id="topicPort" type="text" name="port" size="5" maxlength="5" />
			 
            <label for="topicPath">Path</label>
			<input id="topicPath" type="text" name="path" size="23" /> 
				
			<label for="topicClientId">Client Identifier</label>
			<input id="topicClientId" type="text" name="clientId" size="23" maxlength="256" />
				
			<label for="topicUser">User name</label> 
			<input id="topicUser" type="text" name="userName" size="23" /> 
				
			<label for="topicPassword">Password</label> 
			<input id="topicPassword" type="password" name="password" size="23" /> 
				
			<label for="topicSSL">Use TLS</label> 
			<input id="topicSSL" type="checkbox" name="ssl" /> 
	
		</fieldset>

		<fieldset>
			<legend>Retained Message</legend>
	
			<label for="retainedMsgTopic">Topic</label>
			<input id="retainedMsgTopic" type="text" name="retainedMsgTopic" size="40" />
				
			<input type="button" type="button" style="clear: left; margin:5px 5px 0px 0px" value="Get Retained" onclick="retrieveRetainedMessage()" name="getRetainedMsg" /> 
			<input id="deleteRetained" type="button" style="margin-top: 5px" value="Delete Retained" onclick="deleteRetainedMsg()" name="deleteRetained" />
		</fieldset>

		<fieldset>
			<legend>Publish</legend>
				
			<label for="publishTopic">Topic</label>
			<input id="publishTopic" type="text" name="topicName" size="40" /> 
				
			<label for="publishPersist">Persist</label> 
			<input id="publishPersist" type="checkbox" name="publishPersist" /> 
				
			<label for="publishRetain">Retain</label> 
			<input id="publishRetain" type="checkbox" name="publishRetain" /> 
			 	
			<label for="publishMessage">Message</label>				
			<textarea id="publishMessage" name="textMessage" onKeyUp="resetFileInput()" rows="2" cols="100"></textarea>
				
			<input type="button" style="clear: left; margin-top: 5px"  value="Publish" onClick="publishMessageTopic()"  name="publishTopicButton" />
		</fieldset>
	</form>

	<form id="traceForm" class="tabContent">
		<fieldset>
			<label for="sessionTrace">Trace Log</label> 			
			<textarea id="sessionTrace" name="hist" rows="12" cols="100" readonly="readonly"></textarea>
			<input type="button" style="clear: left; margin: 5px 0px 0px 0px" name="histclear" value="Clear Trace" onclick="traceClear()" /> <br />
		</fieldset>
	</form>

	</div>
	
	<div class="demoFooter">
		<div class="demoBlueLip"></div>
		<div class="demoFooterContent">&copy; Copyright Contributors to the Eclipse Foundation 2014-2021</div>
	</div>
	
</body>
</html>